package com.lkm.market2_0.m_imple.app_main;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskManager;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.TaskIdIndex;
import com.lkm.market2_0.entity.OrganizationInfo;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.app_main.AppMainModule;
import com.lkm.market2_0.m.dataUp.DataUpModule;
import com.lkm.market2_0.netapi.Api;
import com.lkm.market2_0.task.AsyncTask2;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;




 class LoadAppInitForSTask extends AsyncTask2<String,String,ResponEntity<Map<Integer,OrganizationInfo>>> {

    public LoadAppInitForSTask(ITaskExecutor<String, String, ResponEntity<Map<Integer,OrganizationInfo>>> taskExecutor, TaskManager mTaskManager) {
//        LoadAppInitForSTask.class.getName()
        super(taskExecutor, TaskIdIndex.LoadAppInitForSTask, mTaskManager);
    }






    public static boolean isLoading(){
        return ExpandConfig.instance.getTaskManager2().getRunTask(TaskIdIndex.LoadAppInitForSTask)!=null;

    }
    public static LoadAppInitForSTask load(String pushToken,boolean isWatchOk){

        int ts[]= ExpandConfig.instance.getTaskManager2().getRunTask(TaskIdIndex.LoadAppInitForSTask);
        if(ts!=null) {
            Task task= ExpandConfig.instance.getTaskManager2().getRunTask(ts[0]);
            task.cancel();
        }
        LoadAppInitForSTask loadAppInitForSTask=new LoadAppInitForSTask(new DefaultTaskExecutor<String, String, ResponEntity<Map<Integer,OrganizationInfo>>>(true), ExpandConfig.instance.getTaskManager2());
        loadAppInitForSTask.action();
        loadAppInitForSTask.setParam(pushToken);
        loadAppInitForSTask.isWatchOk=isWatchOk;
        loadAppInitForSTask.execTask();
        return loadAppInitForSTask;
    }

    private boolean isWatchOk = false;

    @Override
    protected void doAsyncExecuting(String pushToken) {
        String json = ExpandConfig.instance.interceptNetApi(getId());
        if(json == null)
        json = Api.loadInitInfo(pushToken,LoadAppInitForSTask.this);

        ResponEntity<List<OrganizationInfo>> re=ResponEntity.fromJson(
                json
                ,new com.google.gson.reflect.TypeToken<List<OrganizationInfo>>() {}.getType(),""
        );

        json = ExpandConfig.instance.instask(getId(),re,json);
        if (json != null) {
            re = ResponEntity.fromJson(
                    json
                    ,new com.google.gson.reflect.TypeToken<List<OrganizationInfo>>() {}.getType(),""
            );
        }

        ResponEntity<Map<Integer,OrganizationInfo>> r =new ResponEntity<>(re);

        if(re.isSuccess()){
            Map<Integer,OrganizationInfo> map=new HashMap();
            List<OrganizationInfo> list= re.getData();




            


            for (OrganizationInfo item:list){
                long lastOrderCount=0;
                
                if(StringHelp.isEmpty(item.getNodeCode())){
//                    item.setNodeCode(astOrganizationInfo.getNodeCode());


                }else {
                    if (StringHelp.length(item.serverLastOrderNum) == ExpandConfig.orderNumLength) {
                        lastOrderCount = StringHelp.toLong(item.serverLastOrderNum.substring(ExpandConfig.nodeCodeLength, ExpandConfig.orderNumLength), 0);
                    }else{
                        
                        lastOrderCount= ExpandConfig.initOrderCount;
                    }

                    
                    long locallastOrderCount=0;
                    String localordernum= ExpandConfig.instance.loadValue("ordernum_"+item.getNodeCode(),"0");
                    if (StringHelp.length(localordernum) == ExpandConfig.orderNumLength) {

                        P.p("===本地订单号："+localordernum);
                        locallastOrderCount = StringHelp.toLong(localordernum.substring(ExpandConfig.nodeCodeLength, ExpandConfig.orderNumLength), 0);
                    }else{
                        
                        lastOrderCount= ExpandConfig.initOrderCount;
                    }

                    lastOrderCount=Math.max(locallastOrderCount,lastOrderCount);

                    item.setLastOrderCount(lastOrderCount);
                }


                map.put(item.getId(),item);
            }
            OrganizationInfo organizationInfo = new OrganizationInfo();
            organizationInfo.setName("台秤无登记大市场");
            organizationInfo.setAccessName("THH");
            organizationInfo.setContactMan("刘先生");
            organizationInfo.setContactPhone("13016009988");
            organizationInfo.setAddress("无登记");
            organizationInfo.setLogo("?2018011045");
            organizationInfo.setI("/upload/201710/24/2017102417510121271.png?2018011045");
            organizationInfo.setNodeCode("22222");

            map.put(-4711188, organizationInfo);

            AppDataCache.getInstance().setOrganizationEntity(map);

            r.setData(map);
        }else if(isWatchOk) {
            try {
                Thread.sleep(6 * 1000);
            }catch (Exception e){}
            doExecuting(pushToken);
            return;
        }


        complete(r);
    }

    @Override
    public void onFinish(ResponEntity<Map<Integer,OrganizationInfo>> responEntity) {
        if(responEntity.isSuccess()){
            ExpandConfig.instance.getModuleManager().loadModule(AppMainModule.class);
            
            ExpandConfig.instance.getModuleManager().loadModule(DataUpModule.class);
        }else{
            this.action();
            this.execTask();
        }
        super.onFinish(responEntity);
    }
}
